﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using SelectorDevice;
using WPFMediaKit.DirectShow.Controls;
using System.Windows.Threading;

namespace VideoChatClient
{
	public partial class Page_ClientMain
	{
        
		public Page_ClientMain()
		{
			this.InitializeComponent();
            DataContext = App.ViewModel;
            DispatcherTimer tmr = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(5) };
            tmr.Tick += (_1, _2) =>
                            {
                                if (MultimediaUtil.VideoInputDevices.Any())
                                {
                                    if (App.ViewModel.CurCam==null)
                                        App.ViewModel.CurCam = MultimediaUtil.VideoInputDevices[0];
                                    WebCam.VideoCaptureDevice = App.ViewModel.CurCam;
                                } 
                                //WebCam.
                                tmr.Stop();
                            };
            Loaded += (_1, _2) =>
            {
                if (App.ViewModel.ClientType == RemotingMessage.ClientTypes.Extended)
                {
                /*    conference.Visibility = System.Windows.Visibility.Visible;
                    cr_1.DataContext = App.ViewModel.ConferenceRoom[1];
                    cr_2.DataContext = App.ViewModel.ConferenceRoom[2];
                    cr_3.DataContext = App.ViewModel.ConferenceRoom[3];*/
                    tmr.Start();
                }
            };
           
            
		}
         
        private void WebCam_NewVideoSample(object sender, WPFMediaKit.DirectShow.MediaPlayers.VideoSampleArgs e)
        {
            MemoryStream ms = new MemoryStream();
            e.VideoFrame.RotateFlip(RotateFlipType.Rotate180FlipX);
            e.VideoFrame.Save(ms, ImageFormat.Jpeg);
            App.ViewModel.MainDispatcher.BeginInvoke((Action<byte[]>)(t => { App.ViewModel.MyVideoFrame = t.Clone() as byte[]; }), ms.ToArray());
 //           App.ViewModel.BMP_FromCam = e.VideoFrame;

        }

        private void btn_Test_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            DeviceExch DeviceControl = App.ViewModel.DeviceControl;
            if (DeviceControl.StatCon == DeviceExch.StatusConnect.Connected)
            {
                byte[] BufB = new byte[4];
             /*   for (int i = 0; i < BufB.Length; i++)
                    BufB[i] = 3;
                DeviceControl.TaskCommand(SelectorDevice.DeviceExch.CommandName.ConfStatus, BufB);
                */

                BufB = new byte[30];
                for (int i = 0; i < BufB.Length; i++)
                    BufB[i] = 1;
                DeviceControl.TaskCommand(SelectorDevice.DeviceExch.CommandName.LightSend, BufB);
            }
        	// TODO: Add event handler implementation here.
        }
	}
}